package n91_异步设计模式_工作线程;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author fu
 * @date 2022-03-31 23:01
 * 概要：
 * ---------------------------------------------------------------
 * |                                                              |
 * |                  |
 * |                                                             |
 * |=============================================================
 */
@Slf4j(topic = "c.T1")
public class T1_饥饿 {

    static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡翅");
    static Random RANDOM = new Random();
    static String cooking(String who) throws InterruptedException {
        log.debug("{}做菜中......", who);
        Thread.sleep(3000);
        return MENU.get(RANDOM.nextInt(MENU.size()));
    }

    /**
     * 两个
     * @param args
     */
    public static void main(String[] args) {

//        _test_hunger();
        _test_hunger_solve();

    }

    /**
     * 饥饿 现象 【一个线程 [-依赖-]于 另一个线程的结果】
     * 两个线程都在 点餐--等待--另一个相乘去做菜， 没有人去做菜
     */
    private static void _test_hunger() {
        ExecutorService EEEE = Executors.newFixedThreadPool(2);

        EEEE.execute(() -> {
            log.debug("A处理点餐...");
            Future<String> f = EEEE.submit(() -> {
                log.debug("B做菜");
                return cooking("B");
            });
            // 等待 submit线程 做菜完毕。
            try {
                log.debug("A上菜: {}", f.get());

            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });

        EEEE.execute(() -> {
            log.debug("B处理点餐...");
            Future<String> f = EEEE.submit(() -> {
                log.debug("A做菜");
                return cooking("A");
            });
            try {
                log.debug("B上菜: {}", f.get());

            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 解决饥饿 ， [--- 不同的 任务 使用不同的线程池 ---]
     */
    private static void _test_hunger_solve(){
        ExecutorService SSSSSS = Executors.newFixedThreadPool(1);
        ExecutorService CCCCCC = Executors.newFixedThreadPool(1);

        SSSSSS.execute(() -> {
            log.debug("A处理点餐...");
            Future<String> f = CCCCCC.submit(() -> {
                log.debug("B做菜");
                return cooking("B");
            });
            try {
                log.debug("A上菜: {}", f.get());

            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });

        SSSSSS.execute(() -> {
            log.debug("B处理点餐...");
            Future<String> f = CCCCCC.submit(() -> {
                log.debug("A做菜");
                return cooking("A");
            });
            try {
                log.debug("B上菜: {}", f.get());

            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
    }

}
